package com.zlll.winner.controller;

import com.alibaba.fastjson.JSONObject;
import com.zlll.winner.business.model.mall.MallOrder;
import com.zlll.winner.business.service.mall.IMallOrderService;
import com.zlll.winner.business.service.mall.IMallSeckillManageService;
import com.zlll.winner.business.service.mall.IMallSelectGoodsService;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RequestMapping("api/mall")
@RestController
public class MallController {

    @Reference(version = "${dubbo.consumer.version}")
    private IMallSelectGoodsService selectGoodsService;

    @Reference(version = "${dubbo.consumer.version}")
    private IMallSeckillManageService seckillManageService;

    @Reference(version = "${dubbo.consumer.version}")
    private IMallOrderService orderService;

    /**
     * 星选列表
     * @param data
     * @return
     */
    @PostMapping("selectGoodsList")
    public Object selectGoodsList(@RequestBody JSONObject data) {
        Integer pageSize = data.getInteger("pageSize") == null ? 10 : data.getInteger("pageSize");
        Integer pageNo = data.getInteger("pageNo") == null ? 1 : data.getInteger("pageNo");
        return selectGoodsService.getSelectGoodsPage(null,null,pageSize,pageNo);
    }

    /**
     * 秒杀列表
     * @return
     */
    @GetMapping("seckillGoodsList")
    public Object seckillGoodsList(){
        return seckillManageService.getSeckillList();
    }

    /**
     * 添加订单
     * @param data
     * @return
     */
    @PostMapping("addOrder")
    public Object addOrder(@RequestBody MallOrder data) throws Exception{

        MallOrder mallOrder = orderService.saveMallOrder(data);
        return orderService.getMallOrderVoById(mallOrder.getId());
//        return mallOrder;
    }

    /**
     * 支付订单
     * @param data
     * @return
     */
    @PostMapping("orderPay")
    public Object orderPay(@RequestBody JSONObject data) throws Exception{
        Long id = data.getLong("id");
        String address = data.getString("address");
        String consignee = data.getString("consignee");
        String contact = data.getString("contact");
        return orderService.orderPay(id,address,consignee,contact);
    }

    /**
     * 订单列表
     * @param data
     * @return
     */
    @PostMapping("orderList")
    public Object orderList(@RequestBody JSONObject data){
        Integer pageSize = data.getInteger("pageSize") == null ? 10 : data.getInteger("pageSize");
        Integer pageNo = data.getInteger("pageNo") == null ? 1 : data.getInteger("pageNo");
        String account = data.getString("account");
       return orderService.getMallOrderVoPage(account,pageSize,pageNo,null,null,null,null,null);
    }

    /**
     * 订单详情
     * @param data
     * @return
     */
    @PostMapping("orderDetail")
    public  Object orderDetail(@RequestBody JSONObject data){
        Long id = data.getLong("id");

        return orderService.getMallOrderVoById(id);
//        return null;
    }
}
